<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<%
	String path = request.getScheme() + "://" + request.getServerName()
			+ ":" + request.getServerPort() + request.getContextPath()
			+ "/";
	request.setAttribute("path", path);
%>
<base href="<%=path%>"></base>
<title>杆塔管理</title>
<link rel="stylesheet" type="text/css"
	href="jquery-easyui/themes/default/easyui.css">
<link rel="stylesheet" type="text/css"
	href="jquery-easyui/themes/icon.css">
<script type="text/javascript" src="jquery-easyui/jquery.min.js"></script>
<script type="text/javascript" src="jquery-easyui/jquery.easyui.min.js"></script>
</head>
<body>
	<!-- datagrid 显示数据 -->
	<table id="dg" style="width: 820px; height: 250px"
		data-options="rownumbers:true,singleSelect:true,pagination:true,method:'get'">
		<thead>
			<tr>
				<th data-options="field:'polecode',width:150">杆塔编号</th>
				<th data-options="field:'linename',width:280">所属路线</th>
				<th
					data-options="field:'isstart',width:150,align:'right',formatter:changestate">状态（启用/停用）</th>
				<th data-options="field:'op',width:250,align:'center',formatter:op">操作</th>
			</tr>
		</thead>
	</table>
	<!-- 辅助的小按钮 -->
	<div id="toolbar">
		<a href="javascript:void(0)" class="easyui-linkbutton"
			iconCls="icon-add" plain="true" onclick="addPole()">New Pole</a>
		所属路线: <input id="linecc" class="easyui-combobox" name="dept"
			width="100px" data-options="valueField:'key',textField:'value'"
			panelHeight='auto'> 所是否启用: <input id="usecc"
			class="easyui-combobox" name="dept" width="100px"
			data-options="valueField:'key',textField:'value'" panelHeight='auto'>
		<input type="button" value="查询" onclick="search()">
	</div>
	<!--修改小窗口  -->
	<div id="add_updateDD" class="easyui-dialog" title="修改/新增杆塔"
		style="width: 400px; height: 200px;"
		data-options="iconCls:'icon-save',resizable:true,modal:true,closed: true">
		<form id="aad_updateFF" method="post">
			<div>
				<s:textfield class="easyui-validatebox" name="psPole.polecode"
					data-options="required:true,validType:'isExist'"
					label="杆塔标号" id="polecode" />
			
			</div>
			<div>
				<s:radio list="#{'1':'启用','-1':'停用' }" name="psPole.isstart"
					label="启用状态"></s:radio>
			</div>
		</form>
	</div>
	<script type="text/javascript">
		//对停用、修改、删除操作格式化
		function op(val, row) {
			var id = row.id;
			var s;
			if (row.isstart == -1) {
				s = '停用';
			} else {
				s = '启用';
			}
			return "<a href='javascript:void(0)' onclick='togglePole("
					+ id
					+ ")'>"
					+ s
					+ "|</a><a href='javascript:void(0)' onclick='updatePole("
					+ id
					+ ")'>修改|</a><a href='javascript:void(0)' onclick='deletePole("
					+ id + ")'>删除|</a>";
		}
		//停用/启用杆塔
		function togglePole(id) {
			$.ajax({
				url : 'pole/togglePole',
				data : {
					'psPole.id' : id
				},
				dataType : 'json',
				success : function(data) {
					showMesage(data.message);
					$('#dg').datagrid('reload');
				}
			});
		}
		function showMesage(msg) {
			$.messager.show({
				title : '执行结果',
				msg : msg,
				showType : 'show'
			});
		}
		//新增
		function addPole() {
			$('#add_updateDD').dialog({
				buttons : [ {
					text : 'Cancel',
					handler : function() {
						$('#add_updateDD').dialog('close');
					}
				}, {
					text : 'Save',
					iconCls : 'icon-save',
					handler : function() {
						//表单提交
						$('#aad_updateFF').form({
							url : 'pole/addUpdatePole',
							onSubmit : function() {
								var polecode = $('#polecode').val();
							},
							success : function(data) {
								var data = eval('(' + data + ')');
								showMesage(data.message);
								$('#dg').datagrid('reload');
							}
						}).submit();

					}
				} ]
			}).dialog('open');
		}

		//修改杆塔
		function updatePole(id) {
			$.ajax({
				url : 'pole/findPole',
				data : {
					'psPole.id' : id
				},
				dataType : 'json',
				success : function(data) {
					//表单数据填充
					$('#aad_updateFF').form('load', {
						'psPole.polecode' : data.polecode,
						'psPole.isstart' : data.isstart
					});
				}
			});
			$('#add_updateDD').dialog({
				buttons : [ {
					text : 'Cancel',
					handler : function() {
						$('#add_updateDD').dialog('close');
					}
				}, {
					text : 'Save',
					iconCls : 'icon-save',
					handler : function() {
						//表单提交
						$('#aad_updateFF').form('submit', {
							url : 'pole/addUpdatePole',
							onSubmit : function(psPole) {
								psPole['psPole.id'] = id;
							},
							success : function(data) {
								var data = eval('(' + data + ')');
								showMesage(data.message);
								$('#dg').datagrid('reload');
							}
						});

					}
				} ]
			}).dialog('open');
		}
		function deletePole(id) {
			$.ajax({
				url : 'pole/deletePole',
				data : {
					'psPole.id' : id
				},
				dataType : 'json',
				success : function(data) {
					showMesage(data.message);
					$('#dg').datagrid('reload');
				}
			});
		}
		function changestate(val, row) {
			if (row.isstart == true) {
				return "启动";
			} else {
				return "停用";
			}
		}
		//查询
		function search() {
			var line = $('#linecc').combobox('getValue');
			var isstart = $('#usecc').combobox('getValue');
			$('#dg').datagrid('reload', {
				'psPole.lineid' : line,
				'psPole.isstart' : isstart

			});
		}
		//初始化
		$(function() {
			$('#dg').datagrid({
				toolbar : '#toolbar',
				url : 'pole/showPole'
			});
			$('#usecc').combobox({
				data : [ {
					key : '0',
					value : '请选择'
				}, {
					key : '1',
					value : '启用'
				}, {
					key : '-1',
					value : '停用'
				} ]
			}).combobox('select', '0');
			$('#linecc').combobox({
				url : 'line/getLine'
			}).combobox('setValue', '请选择');
			
			$.extend($.fn.validatebox.defaults.rules, {    
			    isExist: {    
			        validator: function(value, param){
			        	var d;
			        	$.ajax({
							url : 'pole/findPoleCode',
							async: false,//是否异步执行
							data : {
								'psPole.polecode' : value
							},
							dataType : 'json',
							success : function(data) {
								d=data;
							}
						});
			        	return !d.success;
			        },    
			        message: '已存在.'   
			    }    
			});  
		})
	</script>
</body>
</html>